﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EggOfPanKu.Library.Objects;

namespace DeckBuilder
{
    /// <summary>
    /// Interaction logic for CardControl.xaml
    /// </summary>
    public partial class CardControl : UserControl
    {
        private Card _Card;
        public Card Card
        {
            get { return _Card; }
            set { _Card = value; }
        }

        private int _ControlID;
        public int ControlID
        {
            get { return _ControlID; }
            set { _ControlID = value; }
        }

        private bool _isDragging = false;
        public bool IsDragging
        {
            get { return _isDragging; }
            set { _isDragging = value; }
        }

        private Point _startpoint;
        public Point StartPoint
        {
            get { return _startpoint; }
            set { _startpoint = value; }
        }

        public CardControl()
        {
            InitializeComponent();
        }
        public CardControl(Card cardInput)
        {
            Card = cardInput;
            InitializeComponent();
            InitializeCardImage();
        }

        public void InitializeCardImage()
        {
            if (Card != null)
            {

                bool imageExists = false;
                string imagePath = "";

                List<string> images = Card.ImagePath;
                for (int i = images.Count-1; i > -1; i--)
                {
                    if (System.IO.File.Exists(DeckBuilder.App.RootPath + images[i]))
                    {
                        imageExists = true;
                        imagePath = DeckBuilder.App.RootPath + images[i];
                        break;
                    }
                }

                if (!imageExists)
                {
                    //TODO: Set to some default image based on card type
                    if (Card.DeckType == CardDeck.Dynasty)
                    {
                        imagePath = DeckBuilder.App.RootPath + "images\\dynasty_back.jpg";
                    }
                    else
                    {
                        imagePath = DeckBuilder.App.RootPath + "images\\fate_back.jpg";
                    }
                }

                //Load Image
                BitmapImage bmpImage = new BitmapImage();
                bmpImage.BeginInit();
                bmpImage.UriSource = new Uri(imagePath);
                bmpImage.EndInit();
                CardImage.Source = bmpImage;
            }
        }

        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && IsDragging)
            {
                Point position = e.GetPosition(CardImage);
                if (Math.Abs(position.X - StartPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - StartPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    DataObject data = new DataObject("CardControl", this);
                    DragDrop.DoDragDrop(this, data, DragDropEffects.Move);
                }
            }
        }
        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IsDragging = true;
            //this.CaptureMouse();
            StartPoint = e.GetPosition(CardImage);
        }

        private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsDragging = false;
            this.ReleaseMouseCapture();
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            PreviewWindow pw = new PreviewWindow();
            pw.LoadCard(Card);
            pw.Show();
        }

        //Delegate used to bubble Menu Item events up to the page
        public delegate void AddCardHandler(object sender, RoutedEventArgs e);
        //Event used to instantiate the delegate, and call it
        public event AddCardHandler AddCard;
        public void Add_Click(object sender, RoutedEventArgs e)
        {
            AddCard(this, e);
        }

        //Delegate used to bubble Menu Item Change events up to the page
        public delegate void RemoveCardHandler(object sender, RoutedEventArgs e);
        //Event used to instantiate the delegate, and call it
        public event RemoveCardHandler RemoveCard;
        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            RemoveCard(this, e);
        }

        //Delegate used to bubble Menu Item Change events up to the page
        public delegate void ViewCardHandler(object sender, RoutedEventArgs e);
        //Event used to instantiate the delegate, and call it
        public event ViewCardHandler ViewCard;
        private void View_Click(object sender, RoutedEventArgs e)
        {
            ViewCard(this, e);
        }
    }
}
